/*
 * @Author: shawnxiao
 * @Date: 2021-10-26 18:30:40
 * @LastEditTime: 2021-11-08 14:54:24
 * @FilePath: /gatt-react/src/index/views/Home/components/GattDrag/components/GattDate/utils.ts
 */
  import { timestampToTime } from '@/common/utils'

  // 根据时间计算left
  /**
   * @param  {number} startTimeStamp
   * @param  {number} currentYear
   * @param  {number} currentDaySizeVal
   */
    export const computedLeft = (params:any) => {
      const {startTimeStamp, currentYear, currentDaySizeVal } = params
      const startTimeYear = new Date(startTimeStamp).getFullYear()
      const startYear = currentYear - 1 > startTimeYear ? startTimeYear : currentYear - 1
      const curLeft = (startTimeStamp - new Date(`${startYear}/01/01 00:00`).getTime()) / (1000 * 60 * 60 * 24) * currentDaySizeVal
      return curLeft
    }

  // 根据距离计算时间
  /**
   * @param  {Number} width
   * @param  {Boolean|String} time
   * @param  {number|String} startTime 整体的开始时间
   * @param  {number} currentYear
   * @param  {object} currentDaySize
   */
  export const computedWithTime = (params:any) => {
    const { width, time, startTime, currentYear, currentDaySize } = params
    const startTimeYear = new Date(startTime).getFullYear()
    let startYear = 0
    if (currentYear - 1 > startTimeYear) {
      startYear = startTimeYear
    } else {
      startYear = currentYear - 1
    }
    const startTimeTmp =
      (width / currentDaySize.value) * (1000 * 60 * 60 * 24) +
      new Date(`${startYear}/01/01`).getTime()
    const s = new Date(startTimeTmp)
    if (time && time === true) {
      return s.getTime()
    } else if (time && time === 'YD') {
      let m:number|string = s.getMonth() + 1
      m = m < 10 ? `0${m}` : m
      return `${s.getFullYear()}/${m}`
    }
      let m:number|string = s.getMonth() + 1
      m = m < 10 ? `0${m}` : m
      let d:number|string = s.getDate()
      d = d < 10 ? `0${d}` : d
      return `${s.getFullYear()}/${m}/${d}`
  }

      /**
   * @description 获取该月下所有的天和周的数据情况
   * @param  {Number} days 该年下天数总计
   * @param  {Number} year
   */
  export const handleMonthDay = (days:number, year:number, monthArr:number[]) => {
    const arr = []
    const obj = {}
    monthArr.forEach(item => {
      obj[item] = {
        dayArr: [],
        weekArr: []
      }
      let ob:any = {}
      // 该月的天数总计
      ob.totalDay = checkMonthDays(item, days)
      obj[item].dayArr.push(ob)
      ob = {}
    })
    arr.push(obj)
    const s = arr[0]
    for (const item in s) {
      const num = s[item].dayArr[0].totalDay
      const tempArr = getWeekDateByMonth(num, ~~item, year)
      tempArr.forEach(keyItem => {
        if (keyItem.weekday === 1) {
          s[item].weekArr.push(keyItem.date)
        }
      })
      s[item].dayArr = []
      s[item].dayArr = tempArr
    }
    return arr
  }
      // 根据月份返回天数
  /**
   * @param  {Number} month 月份
   * @param  {Number} days 天数
   */
  export const checkMonthDays = (month:number, days:number) => {
    const days31Arr = [1, 3, 5, 7, 8, 10, 12]
    const days30Arr = [4, 6, 9, 11]
    return days31Arr.includes(month) ? 31 : days30Arr.includes(month) ? 30 : days === 365 ? 28 : 29
  }
    /**
   * @description 获取月份下所有天的日期和星期几
   * @param  {number} num 天数
   * @param  {number} month 月份
   * @param  {number} year 年份
   */
  export const getWeekDateByMonth = (num:number, month:number, year:number) => {
    const arr = []
    let obj:any = {}
    const quarterArr = [1, 4, 7, 10]
    const halfYearArr = [1, 7]
    for (let i = 1; i <= num; i++) {
      if (quarterArr.includes(~~month) && i === 1) {
        // 季度开始日/分割线
        obj.quarterStart = true
      }
      if (halfYearArr.includes(~~month) && i === 1) {
        // 半年开始日/分割线
        obj.halfYearStart = true
      }
      obj.year = year
      obj.month = ~~month
      obj.date = i
      obj.weekday = new Date(`${year}/ ${month} / ${i}`).getDay()
      arr.push(obj)
      obj = {}
    }
    return arr
  }

  /**
 * @description 获取以进度数字为key的颜色对象中最小的选项
 * @param  {Array} arrData 进度数字数组 例：[10,20,50,80,100]
 * @param  {number} per 进度
 */
export const getPerColor = (arrData:any[], per:number) => {
  const arr = Array.from(new Set(arrData))
  const newArray = []
  let j = 0
  for (const i in arr) {
    if (~~arr[i] <= per) {
      newArray[j++] = arr[i]
    }
  }
  return Math.max(...newArray)
}

// 根据父级id计算group的宽度,left,startTime和endTime
export const computedGroupWidth = (params:any) => {
  const {curItem, list, startTime, currentYear, currentDaySize} = params
  const parentItem = list.find((a:any) => a.id === curItem.parentId)
  const childArr:any[] = []
  list.length && list.forEach((item:any) => {
    if (item.parentId === curItem.parentId){
      childArr.push(item)
    }
  })
  loop(parentItem, childArr)
  function loop(parent:any, children:any[]) {
    const left = Math.min.apply(
      Math,
      children.map((o:any) => {
        return o.left
      })
    )
    const arrWidth:number[] = []
    children.forEach(item => {
      arrWidth.push(item.left + item.widthMe)
    })
    const width = Math.max.apply(Math, arrWidth)
    const widthMe = width - left
    parent.widthMe = parent.widthChild = widthMe
    parent.left = left
    // 核心改变时间
    const startTimeStamp = computedWithTime({width: left, time: true, startTime, currentYear, currentDaySize})
    const endTimeStamp = computedWithTime({width: left + widthMe, time: true, startTime, currentYear, currentDaySize})
    parent.startTime = startTimeStamp
    parent.endTime = endTimeStamp
    if (parent?.params?.startTime){
      parent.params.startTime = timestampToTime(startTimeStamp, 'second')
    }
    if (parent?.params?.endTime){
      parent.params.endTime = timestampToTime(endTimeStamp, 'second')
    }
    // 如果父级还有父级递归继续
    if (parent.parentId) {
      const parentItem2 = list.find((a:any) => a.id === parent.parentId)
      const childArr2:any[] = []
      list.length && list.forEach((item2:any) => {
        if (item2.parentId === parent.parentId){
          childArr2.push(item2)
        }
      })
      loop(parentItem2, childArr2)
    }
  }
}

// 递归更改父级的per进度
export const looSetPer = (parentId:number|string, arrData:any[]) => {
  const parentItem:any = arrData.find((a:any) => a.id === parentId)
  const childrenArr:any[] = []
  arrData.forEach((aItem:any) => {
    if (aItem.parentId === parentId) {
      childrenArr.push(aItem)
    }
  })
  let count = 0
  const length = childrenArr.length
  childrenArr.forEach((item:any) => {
    count = count + (item.per / 100) * (1 / length)
  })
  parentItem.per = Math.round(count * 100)
  if (parentItem?.params?.per){
    parentItem.params.per = `${Math.round(count * 100)}%`
  }
  if (parentItem.parentId) {
    //  如果还有父级
    looSetPer(parentItem.parentId, arrData)
  }
}
